package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniu.dto.OrderDto;
import com.woniu.entity.FoodItem;
import com.woniu.entity.FoodOrder;
import com.woniu.service.FoodItemService;
import com.woniu.service.FoodOrderService;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Classname OrderController
 * @Description TODO
 * @Date 2025/7/22 11:46
 * @Created by pc
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private FoodOrderService foodOrderService;
    @Resource
    private FoodItemService foodItemService;
    // [{foodId:1,count:2}]
    @PostMapping("/add")
    public ResponseUtil addOrder(@RequestBody List<FoodItem> items) throws InterruptedException {
        ResponseUtil responseUtil = foodOrderService.addOrder(items);
        return responseUtil;
    }

    @GetMapping("/{orderId}")
    public ResponseUtil findByOrderId(@PathVariable String orderId){
        QueryWrapper<FoodOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        FoodOrder one = foodOrderService.getOne(wrapper, false);
        QueryWrapper<FoodItem> itemQueryWrapper = new QueryWrapper<>();
        itemQueryWrapper.eq("order_id",orderId);
        List<FoodItem> list = foodItemService.list(itemQueryWrapper);
        OrderDto orderDto = new OrderDto(one,list);
        return ResponseUtil.get(ResponseEnum.OK,orderDto);
    }

    @GetMapping("/pay/{orderId}")
    public ResponseUtil pay(@PathVariable String orderId){
        // 先查询 订单状态
        QueryWrapper<FoodOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        FoodOrder one = foodOrderService.getOne(wrapper);
        if(one.getStatus() == 1){
            one.setStatus(2);
            UpdateWrapper updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("order_id",one.getOrderId());
            foodOrderService.update(one,updateWrapper);
            return ResponseUtil.get(ResponseEnum.OK);
        }else{
            return  ResponseUtil.get(ResponseEnum.FAIL);
        }
    }

}
